<?php
function verify($hashInput)
{
	$salt = crypt('emf27musedib20ages', 'saltriotikistic28');
	if ($hashInput == $salt)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function deleteOldFiles($targPath, $daysOld, $hash)
{
	if (verify($hash))
	{
		$dir = ROOT.$targPath;
		$dp = opendir($dir) or die ('Could not open '.$dir);
		while ($file = readdir($dp))
		{
			if ($file != '.')
			{
				if ((filemtime($dir.$file)) < (strtotime('-'.$daysOld.' days')))
				{
					unlink($dir.$file);
				}
			}			
		}
	}
}

function receiveFile($name, $fileName, $ext, $target_path, $hash, $override)
{
	//$name - name of the input box that received the file
	//$fileName - new file name
	//$ext - file extension
	//$override - true/false true to replace.
	$fileNameChecked = $fileName;
	if($override == false)
	{
		for ($i=0; $i>-1; $i++)
		{
			if(file_exists($target_path.$fileName.'.'.$ext))
			{
				$fileNameChecked = $fileName.$i;
				break;
			}
			else
			{
				break;
			}
		}
	}
	
	$check = verify($hash);
	$returns = array();
	$returns['status'] = false; 
	$returns['message'] = 'There was an error uploading the file, please try again.';
	if ($check)
	{
		if (frmFile($name) != 'null')
		{
			
			$search = array(" ", ".", ",");
			$preg   = array("", "_", "");
			$nFileName = str_replace($search, $preg, $fileNameChecked);
			$new_name = $nFileName.'.'.$ext;
			
			$_FILES[$name]['name'] = $new_name;
			$fileLocation = $target_path.basename($_FILES[$name]['name']);
				
			if(move_uploaded_file($_FILES[$name]['tmp_name'], $fileLocation))
			{
				$returns['status'] = true; 
				$returns['message'] = basename( $_FILES[$name]['name']);
			} 
			else
			{
			}
		}
	}
	return $returns;
}

?>